package com.eastfair.venueservice.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceSpecs;
import com.eastfair.venueservice.service.ServiceSpecsService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venueservice.vo.ServiceCategoryVO;
import com.eastfair.venueservice.vo.ServiceSpecsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 服务规格
 * </p>
 *
 * @author lyw
 * @date 2022-07-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceSpecs")
@Api(value = "ServiceSpecs", tags = "服务规格")
@PreAuth(replace = "venueservice:serviceSpecs:")
public class ServiceSpecsController extends SuperController<ServiceSpecsService, Long, ServiceSpecs, ServiceSpecsPageQuery, ServiceSpecsSaveDTO, ServiceSpecsUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ServiceSpecsService serviceSpecsService;

    @Override
    public void handlerResult(IPage<ServiceSpecs> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceSpecs> serviceSpecsList = list.stream().map((map) -> {
            ServiceSpecs serviceSpecs = ServiceSpecs.builder().build();
            //TODO 请在这里完成转换
            return serviceSpecs;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceSpecsList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceSpecsPageQuery> params) {
        ServiceSpecsPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 根据服务分类id查询规格列表
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "根据服务分类id查询规格列表")
    @GetMapping(value = "/getServiceSpecsListByCategoryId/{categoryId}")
    public R<List<ServiceSpecsVO>> getServiceSpecsListByCategoryId(@PathVariable("categoryId") Long categoryId){
        try {
            return R.success(serviceSpecsService.getServiceSpecsListByCategoryId(categoryId));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 保存服务规格
     * @param serviceSpecsDTO
     * @return
     */
    @SysLog("添加服务规格")
    @ApiOperation(value = "保存服务规格")
    @PostMapping(value = "/saveServiceSpecsInfo")
    public R<Boolean> saveServiceSpecsInfo(@RequestBody @Validated(ServiceSpecsDTO.Save.class) ServiceSpecsDTO serviceSpecsDTO){
        try {
            return R.success(serviceSpecsService.saveServiceSpecsInfo(serviceSpecsDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 批量保存服务规格
     * @param ServiceCategoryDTO
     * @return
     */
    @SysLog("批量保存服务规格")
    @ApiOperation(value = "批量保存服务规格")
    @PostMapping(value = "/saveServiceSpecsInfoBatch")
    public R<Boolean> saveServiceSpecsInfoBatch(@RequestBody ServiceCategoryDTO ServiceCategoryDTO){
        try {
            return R.success(serviceSpecsService.saveServiceSpecsInfoBatch(ServiceCategoryDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 删除规格
     * @param id
     * @return
     */
    @SysLog("删除服务规格")
    @ApiOperation(value = "删除规格")
    @GetMapping(value = "/delServiceSpecsById/{id}")
    public R<Boolean> delServiceSpecsById(@PathVariable("id") Long id){
        try {
            return R.success(serviceSpecsService.delServiceSpecsById(id));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 根据服务分类id查询规格列表
     * @param ids ids
     * @return R<List<ServiceSpecsVO>>
     */
    @PostMapping("/getServiceSpecsByIds")
    public R<List<ServiceSpecsVO>> getServiceSpecsByIds(@RequestBody List<Long> ids){
        return R.success(baseService.getServiceSpecsByIds(ids));
    }

}
